package com.pro.common.modules.service.dependencies.upload;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinioUploadHttpUtil {

    public static void main(String[] args) {
        String minioEndpoint = "http://127.0.0.1:9000"; // MinIO 服务器地址
        String bucketName = "baoyi";
        String objectName = "1_upload.png"; // 存储在 MinIO 中的文件名
        String filePath = "C:\\1.png"; // 本地文件路径
        String accessKey = "minioadmin"; // 访问密钥
        String secretKey = "minioadmin"; // 密钥

        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            uploadFile(minioEndpoint, bucketName, objectName, fileInputStream, new File(filePath).length(), accessKey, secretKey);
            System.out.println("文件上传成功！");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("文件上传失败：" + e.getMessage());
        }
    }
    public static void uploadFile(String endpoint, String bucketName, String objectName,
                                  InputStream fileInputStream, long fileLength,
                                  String accessKey, String secretKey) throws IOException {
        try {
            // Initialize MinioClient with provided details
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(endpoint)
                    .credentials(accessKey, secretKey)
                    .build();

            // Upload file to MinIO server
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName) // Bucket name
                            .object(objectName) // Object (file) name on MinIO
                            .stream(fileInputStream, fileLength, -1) // Stream and file length
                            .build());
        } catch (MinioException e) {
            System.err.println("MinioException: " + e);
        } catch (IOException e) {
            System.err.println("IOException: " + e);
        } catch (NoSuchAlgorithmException e) {
            System.err.println("NoSuchAlgorithmException: " + e);
        } catch (InvalidKeyException e) {
            System.err.println("InvalidKeyException: " + e);
        }
    }
//
//    public static void uploadFile(String endpoint, String bucketName, String objectName, InputStream fileInputStream, long fileLength, String accessKey, String secretKey) throws IOException {
//        String uploadUrl = endpoint + "/" + bucketName + "/" + objectName;
//
//        // 创建 URL 和连接
//        URL url = new URL(uploadUrl);
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//        // 设置 HTTP 方法为 PUT，上传文件
//        connection.setRequestMethod("PUT");
//        connection.setDoOutput(true);
//
//        // 设置请求的内容类型为文件
//        connection.setRequestProperty("Content-Type", "application/octet-stream");
//        connection.setRequestProperty("Content-Length", String.valueOf(fileLength));
//
//        // 写入文件流到输出流
//        try (OutputStream out = connection.getOutputStream()) {
//            byte[] buffer = new byte[4096];
//            int bytesRead;
//            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
//                out.write(buffer, 0, bytesRead);
//            }
//        }
//
//        // 执行请求并获得响应
//        int responseCode = connection.getResponseCode();
//        if (responseCode != HttpURLConnection.HTTP_OK) {
//            throw new IOException("上传文件失败，响应码: " + responseCode);
//        }
//    }
}
